package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-01-17
 * Time: 下午 23:29
 */

@WebServlet("/userInfo")
public class UserInfoServlet extends HttpServlet {

    ObjectMapper objectMapper = new ObjectMapper();
    private static int Count ;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String blogID = req.getParameter("blogID");

        //获取登录用户的信息
        HttpSession session = req.getSession(false);
        if(session == null) {
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录!");
            return;
        }

        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录!");
            return;
        }

        //如果有bloID说明是博客详情页,如果没有blogID说明是博客列表页
        if(blogID == null) {

            //防止用户信息在网络传输过程当中被抓包捕获而泄漏密码
            user.setPassWord("");
            resp.setContentType("application/json;charset=utf8");
            String JsonString = objectMapper.writeValueAsString(user);
            resp.getWriter().write(JsonString);

        }else {

            BlogDao blogDao = new BlogDao();
            Blog blog =  blogDao.selectOne(Integer.parseInt(blogID));
            if(blog == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("输入的ID有误!");
                return;
            }

            UserDao userDao = new UserDao();
            //author是博客的作者
            User author = userDao.selectByID(blog.getUserID());
            if(author == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("查找的用户信息不存在!");
                return;
            }

            //判断登录用户和博客作者是不是同一个人,如果是才显示删除按钮,否则不显示.
            if(user.getUserID() == author.getUserID()) {
                author.setIsYourBlog(1);
            }else {
                author.setIsYourBlog(0);
            }

            //防止用户信息在网络传输过程当中被抓包捕获而泄漏密码
            author.setPassWord("");
            resp.setContentType("application/json;charset=utf8");
            String JsonString = objectMapper.writeValueAsString(author);
            resp.getWriter().write(JsonString);

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String blogID = req.getParameter("blogID");

        HttpSession session = req.getSession(false);
        if(blogID == null) {
            //博客列表页
            if(session == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("用户未登录!");
                return;
            }

            User user = (User) session.getAttribute("user");
            if(user == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("用户未登录!");
                return;
            }

            BlogDao blogDao = new BlogDao();
            List<Blog> blogs = blogDao.selectAll();
            Count = 0;

            for (Blog tmp:blogs) {
                if(tmp.getUserID() == user.getUserID()) {
                    Count++;
                }
            }
            resp.setContentType("application/json;charset=utf8");
            String JsonString = objectMapper.writeValueAsString(Count);
            resp.getWriter().write(JsonString);


        }else {
            //博客详情页
            BlogDao blogDao = new BlogDao();
            Blog blog =  blogDao.selectOne(Integer.parseInt(blogID));
            if(blog == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("输入的ID有误!");
                return;
            }

            UserDao userDao = new UserDao();
            User user = userDao.selectByID(blog.getUserID());
            if(user == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("查找的用户信息不存在!");
                return;
            }

            resp.setContentType("application/json;charset=utf8");
            String JsonString = objectMapper.writeValueAsString(Count);
            resp.getWriter().write(JsonString);
        }
    }
}
